/*
 * createTime： 2020/08/05 09:52
 * author： wei.wang
 * description: 网络请求封装
 */

import { baseUrl } from '@/config/env';

let btnLock = false;

function Request(options) {
    if (!options.data) {
        options.data = {};
    }
    // 添加全局参数
    if (!options.data.app) {
        options.data.app = uni.getStorageSync('app') || 'default';
    }
    if (uni.getStorageSync('channel')) {
        options.data.channel = uni.getStorageSync('channel');
    }
	
    return new Promise((resolve, reject) => {
        uni.request({
            ...options,
            header: {
                r: Date.parse(new Date()),
                Authorization: uni.getStorageSync('token') || ''
            },
            success: res => {
				if (res.statusCode === 403) {
					if (!btnLock) {
						btnLock = true;
						uni.showToast({
							title: '登录信息已过期',
							icon: 'none'
						});
						uni.removeStorageSync('token');
						
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/index/index'
							});
						}, 2000);
					}
					return false;
				}
				
                const { code, data, status } = res.data;
                switch (code) {
                    case 1000:
                        resolve(data);
                        break;
                    default:
                        reject(res.data);
                }
            },
            fail: err => {
                reject(err);
            },
            complete: () => {

            }
        });
    });
}

// 服务地址变更
export function Service(value) {
    return function (target) {
        // 基础域名变更
        if (typeof value === 'string') {
            target.prototype.namespace = value;
			target.prototype.proxy = null;
        }
        // 复杂域名变更
        if (typeof value === 'object') {
            let { proxy, namespace, url } = value;
            target.prototype.namespace = namespace;

            if (proxy) {
                target.prototype.proxy = proxy;
                target.prototype.url = url;
            }
        }
    };
}

// 基础请求封装
export class BaseService {
    request(options = {}) {
        let path = '';

        if (process.env.NODE_ENV === 'development') {
            path = this.proxy || baseUrl;
        } else {
            if (this.proxy) {
                path = this.url;
            } else {
                path = baseUrl;
            }
        }

        if (this.namespace) {
            path += '/' + this.namespace;
        }
        options.url = path + options.url;
        options.method = options.method || 'GET';

        if (options.method === 'GET' || options.method === 'get') {
            options.data = options.params;
        }

        return Request(options);
    }
    list(params) {
        return this.request({
            url: '/list',
            params: {
                ...params
            }
        });
    }
    page(params) {
        return this.request({
            url: '/page',
            params: {
                ...params
            }
        });
    }
    add(params) {
        return this.request({
            url: '/add',
            method: 'POST',
            data: {
                ...params
            }
        });
    }
    update(params) {
        return this.request({
            url: '/update',
            method: 'POST',
            data: {
                ...params
            }
        });
    }
    delete(params) {
        return this.request({
            url: '/delete',
            method: 'POST',
            data: {
                ...params
            }
        });
    }
    info(params) {
        return this.request({
            url: '/info',
            params: {
                ...params
            }
        });
    }
}

export { Request };